﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebCalculator
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;
        }

        protected void btnNum1_Click(object sender, EventArgs e)
        {
            txtDisplay.Text += "1";
        }

        protected void btnNum2_Click(object sender, EventArgs e)
        {
            txtDisplay.Text += "2";
        }

        protected void btnNum3_Click(object sender, EventArgs e)
        {
            txtDisplay.Text += "3";
        }

        protected void btnNum4_Click(object sender, EventArgs e)
        {
            txtDisplay.Text += "4";
        }

        protected void btnNum5_Click(object sender, EventArgs e)
        {
            txtDisplay.Text += "5";
        }

        protected void btnNum6_Click(object sender, EventArgs e)
        {
            txtDisplay.Text += "6";
        }

        protected void btnNum7_Click(object sender, EventArgs e)
        {
            txtDisplay.Text += "7";
        }

        protected void btnNum8_Click(object sender, EventArgs e)
        {
            txtDisplay.Text += "8";
        }

        protected void btnNum9_Click(object sender, EventArgs e)
        {
            txtDisplay.Text += "9";
        }

        protected void btnNum0_Click(object sender, EventArgs e)
        {
            txtDisplay.Text += "0";
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Session["FirstOperand"] = decimal.Parse(txtDisplay.Text);
            Session["Operation"] = NumericOperation.Addition;
            txtDisplay.Text = string.Empty;
        }

        protected void btnSub_Click(object sender, EventArgs e)
        {
            Session["FirstOperand"] = decimal.Parse(txtDisplay.Text);
            Session["Operation"] = NumericOperation.Subtraction;
            txtDisplay.Text = string.Empty;
        }

        protected void btnMul_Click(object sender, EventArgs e)
        {
            Session["FirstOperand"] = decimal.Parse(txtDisplay.Text);
            Session["Operation"] = NumericOperation.Multiplication;
            txtDisplay.Text = string.Empty;
        }

        protected void btnDiv_Click(object sender, EventArgs e)
        {
            Session["FirstOperand"] = decimal.Parse(txtDisplay.Text);
            Session["Operation"] = NumericOperation.Division;
            txtDisplay.Text = string.Empty;
        }

        protected void btnSqrt_Click(object sender, EventArgs e)
        {
            try
            {
                Calculator calculator = new Calculator(decimal.Parse(txtDisplay.Text),
                    0, NumericOperation.SquareRoot);

                txtDisplay.Text = calculator.CalculateResult().ToString();
            }
            catch (NullReferenceException)
            {
                lblErrorMessage.Text = "You have not entered one or more operands!";
            }
            catch (FormatException)
            {
                lblErrorMessage.Text = "Invalid input!";
            }
        }

        protected void btnModule_Click(object sender, EventArgs e)
        {
            try
            {
                Calculator calculator = new Calculator(decimal.Parse(txtDisplay.Text),
                    0, NumericOperation.Module);

                txtDisplay.Text = calculator.CalculateResult().ToString();
            }
            catch (NullReferenceException)
            {
                lblErrorMessage.Text = "You have not entered one or more operands!";
            }
            catch (FormatException)
            {
                lblErrorMessage.Text = "Invalid input!";
            }
        }

        protected void btnEquals_Click(object sender, EventArgs e)
        {
            try
            {
                Calculator calculator = new Calculator();
                calculator.FirstOperand = (decimal)Session["FirstOperand"];
                calculator.SecondOperand = decimal.Parse(txtDisplay.Text);
                calculator.Operation = (NumericOperation)Session["Operation"];
                txtDisplay.Text = calculator.CalculateResult().ToString();
            }
            catch (NullReferenceException)
            {
                lblErrorMessage.Text = "You have not entered one or more operands!";
            }
            catch (DivideByZeroException)
            {
                lblErrorMessage.Text = "You cannot divide by zero!";
            }
            catch (FormatException)
            {
                lblErrorMessage.Text = "Invalid input!";
            }
        }

        protected void btnErase_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = string.Empty;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text.Remove(txtDisplay.Text.Length - 1, 1);
        }
    }
}